//
//  UIScrollView+NRWRefresh.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/16.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "UIScrollView+NRWRefresh.h"
#import <objc/runtime.h>

static NSInteger maskViewTag = 430079;

@implementation UIScrollView (NRWRefresh)

- (void)setMaskClick:(void (^)(void))maskClick {
    
    objc_setAssociatedObject(self, &"maskClickBlock", maskClick, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(void))maskClick {
    
    return objc_getAssociatedObject(self, &"maskClickBlock");
}

- (void)addHeaderRefreshWithTarget:(id)target action:(SEL)action {
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:target refreshingAction:action];
    [header setImages:[NRWRefreshImageArray refreshImages] duration:1 forState:MJRefreshStateIdle];
    [header setImages:[NRWRefreshImageArray refreshImages] duration:1 forState:MJRefreshStatePulling];
    [header setImages:[NRWRefreshImageArray refreshImages] duration:1 forState:MJRefreshStateRefreshing];
    header.lastUpdatedTimeLabel.hidden= YES;
    header.stateLabel.hidden = YES;
    self.mj_header = header;
}

- (void)addFooterRefreshWithTarget:(id)target action:(SEL)action {
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:target refreshingAction:action];
	footer.triggerAutomaticallyRefreshPercent = -3;
    [footer setTitle:@"努力加载中" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据啦" forState:MJRefreshStateNoMoreData];
    self.mj_footer = footer;
}

- (void)endRefresh {
    
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
}

- (void)showEmptyMaskViewWithClickBlock:(void (^)(void))didClick {
    
    [self showMaskViewWithTitle:@"暂无符合条件车款，您可添加订阅" imageName:@"default_empty" titleToImageMargin:42 canScroll:NO didClick:didClick];
}

- (void)showMaskViewWithTitle:(NSString *)title imageName:(NSString *)imageName titleToImageMargin:(CGFloat)margin canScroll:(BOOL)canScroll didClick:(void (^)(void))didClick{
    
    //防止叠加
    if ([self viewWithTag:maskViewTag]) {
        return;
    }
    
    [self setMaskClick:didClick];
    
    self.scrollEnabled = canScroll;
    
    UIView *maskView = [[UIView alloc] init];
    maskView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    maskView.backgroundColor = self.backgroundColor == [UIColor clearColor]? [UIColor whiteColor]: self.backgroundColor;
    maskView.tag = maskViewTag;
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = self.backgroundColor == [UIColor clearColor]? [UIColor whiteColor]: self.backgroundColor;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.textColor = getColor(@"#808080");
    titleLabel.font = getThemeFont(17);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:imageName];
    
    [contentView addSubview:titleLabel];
    [contentView addSubview:imageView];
    [maskView addSubview:contentView];
    
    NSDictionary *arrts = @{NSFontAttributeName:titleLabel.font};
    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(SCREEN_WIDTH() - 50, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:arrts context:nil].size;
    [imageView sizeToFit];
    
    imageView.frame = CGRectMake((SCREEN_WIDTH() - imageView.bounds.size.width) / 2, 0, imageView.bounds.size.width, imageView.bounds.size.height);
    titleLabel.frame = CGRectMake(25, imageView.viewHeight + margin, SCREEN_WIDTH() - 50, titleSize.height);
    contentView.frame = CGRectMake(0, (self.frame.size.height - titleSize.height - imageView.bounds.size.height - margin) / 2, SCREEN_WIDTH(), titleSize.height + imageView.bounds.size.height + margin);
    
    [self addSubview:maskView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [maskView addGestureRecognizer:tap];
}

- (void)removeMaskView {
    
    self.scrollEnabled = YES;
    [[self viewWithTag:maskViewTag] removeFromSuperview];
}

#pragma mark - privateMethod
- (void)tapAction {
    
    if (self.maskClick) {
        self.maskClick();
    }
}

@end
